﻿using JESAI.EventBus.ExcutedResults;
using JESAI.EventBus.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JESAI.EventBus.Events
{
    public class PublishEvent : IEvent
    {
        public PublishEvent()
        {
        }

        public PublishEvent(string eventId)
        {
            EventId = eventId;
        }

        public PublishEvent(string eventId, object payload)
            : this(eventId)
        {
            Payload = payload;
        }

        public PublishEvent(string eventId, object payload, CancellationToken cancellationToken)
            : this(eventId, payload)
        {
            CancellationToken = cancellationToken;
        }

        public PublishEvent(string eventId, object payload, DateTime runTime, CancellationToken cancellationToken)
          : this(eventId, payload)
        {
            CancellationToken = cancellationToken;
            RunTime = runTime;
        }

        public PublishEvent(Enum eventId, object payload, DateTime runTime, CancellationToken cancellationToken)
          : this(eventId, payload)
        {
            CancellationToken = cancellationToken;
            RunTime = runTime;
        }

        public PublishEvent(Enum eventId)
            : this(Helper.ParseToString(eventId))
        {
        }

        public PublishEvent(Enum eventId, object payload)
            : this(Helper.ParseToString(eventId), payload)
        {
        }
        public PublishEvent(Enum eventId, object payload, CancellationToken cancellationToken)
            : this(Helper.ParseToString(eventId), payload, cancellationToken)
        {
        }

        public string EventId { get; set; }

        public object Payload { get; set; }

        public DateTime CreatedTime { get; set; }

        [Newtonsoft.Json.JsonIgnore]
        [System.Text.Json.Serialization.JsonIgnore]
        public CancellationToken CancellationToken { get; set; }

        public bool IsConsumOnce { get; set; }

        public DateTime? RunTime { get; set; }

        public bool AllowMultiConsum { get; set; }

        public event EventHandler<EventHandleResult> OnPublishedCallBack;

        public void PublishedCallback(EventHandleResult result)
        {
            try
            {
                OnPublishedCallBack?.Invoke(this, result);
            }
            catch { }
        }


    }
}
